package ldh.im.admin.controller;

import java.util.Date;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import ldh.common.PageResult;
import ldh.common.json.JsonViewFactory;
import ldh.common.mvc.DateEditor;
import ldh.im.admin.constant.AuthorityStatus;
import ldh.im.admin.pojo.Authority;
import ldh.im.admin.pojo.where.AuthorityWhere;
import ldh.im.admin.service.AuthorityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author: ldh
* @date: 2021-06-27 20:39:38
*/
@Controller
@RequestMapping("authority")
public class SysAuthorityController {

	@Resource(name = "authorityService")
	private AuthorityService authorityService;
	
	/**
	 * 字符串转化成时间
	 */
	@InitBinder  
	protected void initBinder(HttpServletRequest request,  
	                              ServletRequestDataBinder binder) throws Exception {  
	    //对于需要转换为Date类型的属性，使用DateEditor进行处理  
	    binder.registerCustomEditor(Date.class, new DateEditor());  
	} 

	@RequiresPermissions("authority:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public String save(@ModelAttribute Authority authority) throws Exception {
    	Objects.requireNonNull(authority);
    	if (authority.getId() == null) {
			authorityService.insert(authority);
    	} else {
			authorityService.updateById(authority);
    	}
    	return "redirect:/authority/list";
    }

	@RequiresPermissions("authority:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save/json")
    @ResponseBody
    public String saveJson(@ModelAttribute AuthorityWhere authority) throws Exception {
    	Objects.requireNonNull(authority);
    	if (authority.getId() == null) {
			authorityService.insert(authority);
		} else {
			authorityService.updateById(authority);
		}
        return JsonViewFactory.create()
				.toJson();
    }

	@RequiresPermissions("authority:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/toEdit/{id}")
    public String toEdit(@PathVariable Long id, Model model) throws Exception {
		Authority authority = authorityService.getById(id);
    	model.addAttribute("authority", authority);
		model.addAttribute("active", "authority");
		model.addAttribute("statusValues", AuthorityStatus.values());

        return "authority/authorityEdit";
    }

	@RequiresPermissions("authority:toAdd")
	@RequestMapping(method = RequestMethod.GET, value = "/toAdd")
    public String toAdd(Model model) throws Exception {
		model.addAttribute("active", "authority");
		model.addAttribute("statusValues", AuthorityStatus.values());
        return "authority/authorityEdit";
    }

	@RequiresPermissions("authority:view")
	@RequestMapping(method = RequestMethod.GET, value = "/view/{id}")
    public String view(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	Authority authority = authorityService.getById(id);
    	model.addAttribute("authority", authority);
		model.addAttribute("active", "authority");
        return "authority/authorityView";
    }

	@RequiresPermissions("authority:edit")
	@ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/view/json/{id}")
    public String viewJson(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	Authority authority = authorityService.getById(id);
    	return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", authority)
				.toJson();
    }

	@RequiresPermissions("authority:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String list(@ModelAttribute AuthorityWhere authorityWhere, Model model) {
		authorityWhere.setOrder("id desc");
		PageResult<Authority> authoritys = authorityService.findByAuthorityWhere(authorityWhere);
		model.addAttribute("authoritys", authoritys);
		model.addAttribute("active", "authority");
		model.addAttribute("authority", authorityWhere);
		model.addAttribute("statusValues", AuthorityStatus.values());
		return "authority/authorityList";
	}

	@RequiresPermissions("authority:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/list/json")
	@ResponseBody
	public String listJson(@ModelAttribute AuthorityWhere authorityWhere, Model model) {
		authorityWhere.setOrder("id desc");
		PageResult<Authority> authoritys = authorityService.findByAuthorityWhere(authorityWhere);
		return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", authoritys)
				.toJson();
	}
	
	@RequiresPermissions("authority:delete")
	@RequestMapping(method = RequestMethod.GET, value = "/deleteById/json/{id}")
	@ResponseBody
	public String deleteJsonById(@PathVariable("id")Long id) {
		Authority authority = authorityService.getById(id);
        Objects.requireNonNull(authority);
		authorityService.deleteById(id);
        return JsonViewFactory.create()
				.toJson();
        }

	
}